package com.kaifamiao.convert;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;

/**
 * 1、java.sql.Date类是java.util.Date类的子类，它只关注年月日
 * 2、java.util.Date ===> LocalDateTime / LocalDate / LocalTime
 */
public class FromDateTest {

	public static void main(String[] args) {
		
		// 根据毫秒值创建一个 java.sql.Date 实例
		java.sql.Date date = new java.sql.Date( System.currentTimeMillis() );
		System.out.println( date );
		// java.sql.Date ==> java.time.LocalDate
		LocalDate localDate = date.toLocalDate();
		System.out.println( localDate );
		
		System.out.println( "- - - - - - - - - - - - - - -" );
		
		final java.util.Date uDate = new java.util.Date();
		// 直接通过 java.util.Date 类中实例方法获得相应的 Instant实例
		Instant instant = uDate.toInstant();
		
		ZoneId zone = ZoneId.of( "Asia/Shanghai" );
		// 根据给定的时区获得ZonedDateTime实例
		ZonedDateTime zdt = instant.atZone( zone );
		
		// java.time.ZonedDateTime ==> LocalDateTime
		LocalDateTime datetime = zdt.toLocalDateTime();
		System.out.println( datetime );
		
		// java.time.ZonedDateTime ==> LocalDate
		LocalDate localdate = zdt.toLocalDate();
		System.out.println( localdate );
		
		// java.time.ZonedDateTime ==> LocalTime
		LocalTime localtime = zdt.toLocalTime();
		System.out.println( localtime );

	}

}
